package cn.yj.core.business.controller;

import cn.yj.annotation.pagehelper.page.OrderBy;
import cn.yj.annotation.pagehelper.page.PageUtils;
import cn.yj.cgs.dba.entity.IntegralGoods;
import cn.yj.cgs.dba.entity.IntegralGoodsType;
import cn.yj.cgs.dba.entity.vo.IntegralGoodsForm;
import cn.yj.admin.frame.AbstractController;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.core.business.service.GoodsService;
import cn.yj.entity.R;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-06-06 20:14
 */
@RestController
@RequestMapping("/goods")
public class IntegralGoodsController extends AbstractController<IntegralGoods> {

    @Resource
    private GoodsService goodsService;

    @Override
    @GetMapping("/pageList")
    @RequiresPermissions("goods:list")
    public R pageList(@RequestParam Map<String, Object> params) {
        return success(goodsService.pageList(params, page(new OrderBy(OrderBy.Direction.DESC, "create_time"))));
    }


    @RequiresPermissions("goods:records:list")
    @GetMapping("/goodsChangeRecordsPageList")
    public R goodsChangeRecordsPageList(@RequestParam Map<String, Object> params) {
        return success(goodsService.goodsChangeRecordsPageList(params, PageUtils.page()));
    }


    @RequiresPermissions("goods:add")
    @PostMapping("/save")
    @OperateLog(describe = "添加积分商品",type = LogType.INSERT)
    public R save(@RequestBody IntegralGoodsForm integralGoods) {
        return result(goodsService.save(integralGoods));
    }

    @RequiresPermissions("goods:update")
    @PostMapping("/update")
    @OperateLog(describe = "修改积分商品",type = LogType.UPDATE)
    public R updateById(@RequestBody IntegralGoodsForm integralGoods) throws Exception {
        return result(goodsService.updateById(integralGoods));
    }

    @RequiresPermissions("goods:changeStatus")
    @PutMapping("/changeStatus")
    @OperateLog(describe = "修改积分商品状态",type = LogType.UPDATE)
    public R changeStatus(@RequestBody Map<String, String> body) {
        return success(goodsService.changeStatus(body));
    }


    @RequiresPermissions(value = {"goods:type:list"}, logical = Logical.OR)
    @GetMapping("/goodsTypePageList")
    public R goodsTypePageList(@RequestParam Map<String, Object> params) {
        return success(goodsService.goodsTypePageList(params, PageUtils.page(new OrderBy(OrderBy.Direction.DESC,
                "create_time"))));
    }

    @GetMapping("/goodsType")
    public R goodsType() {
        return success(goodsService.getGoodsType());
    }


    @RequiresPermissions("goods:type:add")
    @PostMapping("/type/save")
    @OperateLog(describe = "添加积分商品类型")
    public R typeSave(@RequestBody IntegralGoodsType type) {
        return result(goodsService.saveAndUpdateType(type));
    }

    @RequiresPermissions("goods:type:del")
    @DeleteMapping("/type/remove/{nos}")
    @OperateLog(describe = "删除积分商品类型")
    public R typeRemove(@PathVariable String[] nos) {
        return result(goodsService.deletedTypeByNo(nos));
    }

    @RequiresPermissions("goods:del")
    @OperateLog(describe = "删除积分商品")
    @DeleteMapping("/remove/{ids}")
    public R remove(@PathVariable Integer[] ids) {
        return result(goodsService.deletedGoodsById(ids));
    }

    @RequiresPermissions("goods:stock:add")
    @OperateLog(describe = "添加积分商品库存")
    @PutMapping("/addStock")
    public R addStock(@RequestBody Map<String, Object> params) {
        return result(goodsService.addStock(params));
    }


    /**
     * 兑换记录的物流详情
     *
     * @param userGoodsNo 兑换商品的编号
     */
    @RequiresPermissions("goods:record:progress")
    @GetMapping("/getProgress/{userGoodsNo}")
    public R getProgress(@PathVariable String userGoodsNo) {
        return success(goodsService.getProgress(userGoodsNo));
    }


    @RequiresPermissions("goods:record:sendGoods")
    @PutMapping("/sendGoods")
    @OperateLog(describe = "发出积分商品")
    public R sendGoods(@RequestBody Map<String, Object> params) {
        return result(goodsService.sendGoods(params));
    }

    @GetMapping("/getExpressStatus")
    public R getExpressStatus() {
        return success(goodsService.getExpressStatus());
    }
}
